﻿using System.ServiceModel;
using System.ServiceProcess;
using System.Diagnostics;
using System.Threading;
using Clarendon.AzureBilling.Library;

namespace Clarendon.AzureBilling.Service
{
    public partial class AzureService : ServiceBase
    {
        public static EventLog Log;
        public static ServiceHost Host;

        public AzureService()
        {
            InitializeComponent();

            //if (!EventLog.SourceExists("Azure Billing Agent"))
            //{
            //    EventLog.CreateEventSource("Azure Billing Agent", "AzureBillingAgent");
            //}

            timerAzureBillingCheck.Enabled = true;
            timerAzureBillingCheck.Interval = 1000*60*60;
            timerAzureBillingCheck.Tick += new System.EventHandler(timerAzureBillingCheck_Tick);
        }

        void timerAzureBillingCheck_Tick(object sender, System.EventArgs e)
        {
            var loader = new AzureServiceLoader();
            loader.GetLoaded();    
        }

        protected override void OnStart(string[] args)
        {
            Host = new ServiceHost(typeof(AzureBillingService));
            Host.Open();
            timerAzureBillingCheck.Start();
            Trace.WriteLine("OnStart Method Entered");            
        }

        protected override void OnStop()
        {
            timerAzureBillingCheck.Stop();
            Host.Close();
            Trace.WriteLine("OnStop Method Entered");
        }

        protected override void OnContinue()
        {
            Trace.WriteLine("Service has now been continued - starting Azure billing timer");
        }

        protected override void OnPause()
        {
            Trace.WriteLine("Service has been paused - stopping Azure billing timer");
        }
    }
}
